<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'device:management:inFireHost:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsDeviceManagementInFireHostForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { InFireHost, inFireHostSave, inFireHostForm } from '/@/api/device.management/inFireHost';
  import {Office, officeTreeData,officeListData,officeTreeDataList} from '/@/api/sys/office';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('device.management.inFireHost');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InFireHost>({} as InFireHost);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增') : t('编辑'),
  }));
  const EnterpriseParams = ref<Recordable>({
     name:''
  });

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('企业名称'),
      field: 'enterpriseId',
      fieldLabel:'enterpriseName',
      component: 'TreeSelect',
      componentProps: () => {
        return {
          api: unitEnterpriseDominantListData,
          params: EnterpriseParams.value,
          fieldNames: { label: 'name', value: 'unitCode' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            EnterpriseParams.value.name = value;
            console.log(EnterpriseParams.value);
          },
        };
      },
      required: true,
    },
    {
      label: t('设备名称'),
      field: 'equipmentName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      required: true,
    },
    {
      label: t('设备编号'),
      field: 'equipmentCode',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      required: true,
    },
    {
      label: t('设备类型'),
      field: 'equipmentType',
      fieldLabel:'equipmentTypeName',
      component: 'Select',
      componentProps: {
        maxlength: 255,
        dictType: ' host_equipment_type ',
        // fieldNames:{
        //   label:'dictLabel',
        //   value:'dictValue'
        // },
        allowClear:true,
      },
      required: true,
    },
    {
      label: t('卡号'),
      field: 'cardCode',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('检测间隔'),
      field: 'testingInterval',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await inFireHostForm(data);
    record.value = (res.inFireHost || {}) as InFireHost;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
        // equipmentId: record.value.equipmentId,
        // equipmentTypeIdName: record.value.equipmentTypeName,
        // enterpriseId: record.value.enterpriseId,
      };
      // console.log('submit', params, data, record);
      const res = await inFireHostSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
